#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/12/18 16:04
# @Author : ZhengQL
import requests
from common.Rquest_Handler import RequestHandler
from common.Yaml_Handler import YamlHandler
import pytest
from faker import Faker
from datetime import datetime
from common.Path_Handler import PathHandler

# 获取token请求
@pytest.fixture(scope="session")
def get_token_fixture():
    """登陆"""

    print("开始登陆")

    url = YamlHandler(PathHandler.config_path + r'/config.yaml').read_yaml()['login']['url']['url']
    headers = YamlHandler(PathHandler.config_path + r'/config.yaml').read_yaml()['login']['headers']
    # print("YAML Data:", url)  # 打印 url# 内容
    # print("YAML Data:", headers)  # 打印 header 内容

    payload2 = {
        'password': "YGmOb248cLw="
        # 'password': "YGnTbG87BMk="
    }
    # ssl._create_default_https_context = ssl._create_unverified_context

    req = RequestHandler()
    login_res = req.visit("post", url=url, data=payload2, headers=headers)

    # 打印调试
    # print(login_res)

    # 提取 access_token
    access_token = login_res['data']['access_token']

    # 创建会话对象
    session = requests.Session()

    # 将 access_token 保存到会话中
    session.headers.update({'Authorization': f'Bearer {access_token}'})
    # print(access_token)


    token = f'Bearer ' + login_res['data']['access_token']
    print(token)
    return token


# 随机生成时间戳，生成工单名称
@pytest.fixture
def get_keyword():
    fake = Faker().random_int(0000, 9999)
    current_time = datetime.now()
    # 格式化时间为 YYYY-MM-DD HH:MM:SS
    formatted_time = current_time.strftime("%m%d-%H:%M:%S")

    keyword = f'测试{fake}@{formatted_time}'

    # yaml_handler = YamlHandler('../config/test_data.yaml')
    # yaml_handler.write_yaml({'get_keyword': {'name': get_keyword()}})

    return keyword



if __name__ == '__main__':


    token = get_token_fixture
    print(token)
    # print(time.time())




